package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.service.CustomerService;
/**
 * @author ling
 *
 */
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("struts-default")
@Results({@Result(name="list",location="/pages/base/courier.jsp"),
		@Result(name="unauthorizd",location="/unauthorized.jsp")})
@ExceptionMappings({@ExceptionMapping(exception = "org.apache.shiro.authz.UnauthorizedException", result = "unauthorizd")})
public class CourierAction extends BaseAction<Courier> {
	
	/**
	 * 保存员工数据
	 */
	@Autowired
	private CourierService courierService;
	@Action("courierAction_save")
	public String save(){
		courierService.save(model);
		return "list";
	}
	/**
	 * 分页查询员工数据
	 * @return
	 * @throws IOException 
	 */
 @Action("courierAction_pageQuery")
	public String pageQuery() throws IOException{
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Courier> page=courierService.findAll(model,pageable);
		/*Map<String, Object> map=new HashMap<>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		//将map中的数据转成json数据
		String json = JSONObject.fromObject(map).toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(json);
		System.out.println(json);*/
		this.java2Json(page, new String[]{"fixedAreas"});
		return null;
	}
 	private String ids;	
 	public void setIds(String ids) {
		this.ids = ids;
	}
 	/**
 	 * 批量删除
 	 * @return
 	 */
@Action("courierAction_deleteBantch")
 public String deleteBantch(){
	 courierService.deleteBantch(ids);
	 return "list";
 }

/**
 *快递员还原 
 */
@Action("courierAction_restore")
public String restore()throws Exception{
	courierService.restore(ids);
	return "list";
}


/**
 * 关联快递员,准备数据
 */
private String fixedAreaId;

public void setFixedAreaId(String fixedAreaId) {
	this.fixedAreaId = fixedAreaId;
}
@Action("courierAction_listajax")
public String listajax(){
	List<Courier> list=courierService.findNotDeleteAndNotLinked( fixedAreaId);
	this.java2Json(list, new String[]{"fixedAreas"});
	return null;
}
public String areaString;

public void setAreaString(String areaString) {
	this.areaString = areaString;
}


@Autowired
private CustomerService customerProxy;
@Autowired
private FixedAreaService fixedAreaService;
@Action("courierAction_findCourierByFixedId")
	public String findCourierByFixedId(){
	String fixedAreaId = customerProxy.findFixedAreaIdByAddress(areaString);
	/*FixedArea fixedArea=fixedAreaService.findByFixedAreaId(fixedAreaId);
	Set<Courier> couriers = fixedArea.getCouriers();*/
	List<Courier> list=courierService.findNotDeleteAndNotLinked(fixedAreaId);
	this.java2Json(list, new String[]{"fixedAreas"});
	return NONE;
}
}
